package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.zhaoshang.result.ZsProjectSeatRobResult;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.ZhaoShangPb;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.zhaoshang.ZsMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.List;

@Cmd(code = CrossProtocol.C2_ZHAO_SHANG_PROJECT_SEAT_ROB, desc = "项目席位抢占")
public class ZsProjectSeatRobCmd extends CrossCommand {

    private long projectId;
    private int seatId;
    private long seatUserId;
    private UserBaseInfo userBaseInfo;
    private int titleAddition;
    private int addition;   //称号加成
    private int decorationClass;
    private int robNumLimit;
    private int coolDownLimit;
    private int templateId;
    private long projectServerId;
    private long decorationOverTime;
    private List<Integer> timeLimit;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhaoShangProto.CrossZsProjectSeatRobReqMsg reqMsg = ZhaoShangProto.CrossZsProjectSeatRobReqMsg.parseFrom(packet.getBytes());
        projectId = reqMsg.getProjectId();
        seatId = reqMsg.getSeatId();
        seatUserId = reqMsg.getSeatUserId();
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserInfo());
        titleAddition = reqMsg.getTitleAddition();
        addition = reqMsg.getAddition();
        decorationClass = reqMsg.getDecorationClass();
        robNumLimit = reqMsg.getRobNumLimit();
        coolDownLimit = reqMsg.getCoolDownLimit();
        templateId = reqMsg.getTemplateId();
        projectServerId = reqMsg.getProjectServerId();
        decorationOverTime = reqMsg.getDecorationOverTime();
        timeLimit = reqMsg.getTimeLimitList();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ZsProjectSeatRobResult result = new ZsProjectSeatRobResult();
        result.setTitleAddition(titleAddition);
        result.setAddition(addition);
        result.setDecorationClass(decorationClass);
        result.setRobNumLimit(robNumLimit);
        result.setCoolDownLimit(coolDownLimit);
        result.setProjectServerId(projectServerId);
        result.setDecorationOverTime(decorationOverTime);

        ZhaoShangProto.ZsProjectSeatRobRespMsg.Builder crossRespMsg = ZhaoShangProto.ZsProjectSeatRobRespMsg.newBuilder();
        int hours = DateHelper.getHours();
        if(hours >= timeLimit.get(0) && hours < timeLimit.get(1)) {
            ZsMgr.robProjectSeat(packet.getPlayerId(), serverId, projectId, seatId, seatUserId, userBaseInfo, templateId, result);
            crossRespMsg.setLastRobTime(result.getLastRobTime());
        }else {
            result.setRet(GameErrorCode.E_ZS_PROJECT_NO_IN_SEAT_ROB_TIME);
        }
        crossRespMsg.setRet(result.getRet());
        crossRespMsg.setProjectId(projectId);
        crossRespMsg.setSeatId(seatId);
        if(result.getRet() == 0){  //正常pk
            crossRespMsg.setResultType(result.getResultType());
            crossRespMsg.setPkRounds(result.getPkRounds());
            if(result.getEnemyInfo() != null) {
                crossRespMsg.setEnemyInfo(PlayerBasePb.parsePlayerBaseTempMsg(result.getEnemyInfo()));
            }
            if(result.getZsProject() != null){
                crossRespMsg.setProjectInfo(ZhaoShangPb.parseZsProjectInfoTemp(result.getZsProject(), true, packet.getPlayerId()));
            }
            crossRespMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_ZS_PROJECT_SEAT_ROB, crossRespMsg));
    }

    @Override
    public void sendData() {

    }
}
